/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.iasd.youthshelter.wizards.core.events.investiture;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.openide.util.Lookup;
import pt.iasd.youthshelter.datamodel.Club;
import pt.iasd.youthshelter.datamodel.Honor;
import pt.iasd.youthshelter.datamodel.Lider;
import pt.iasd.youthshelter.datamodel.Pathfinder;
import pt.iasd.youthshelter.datamodel.ProgressiveClass;
import pt.iasd.youthshelter.datamodel.service.ClubController;
import pt.iasd.youthshelter.datamodel.service.HonorController;
import pt.iasd.youthshelter.datamodel.service.LiderController;
import pt.iasd.youthshelter.datamodel.service.PathfinderController;
import pt.iasd.youthshelter.datamodel.service.ProgressiveClassController;
import pt.iasd.youthshelter.wizards.core.ListSorter;
import pt.iasd.youthshelter.wizards.core.WizardKeys;

public final class InvestitureDetailsVisualPanel extends JPanel implements DocumentListener, ActionListener {
    private static final ResourceBundle bundle = java.util.ResourceBundle.getBundle("pt/iasd/youthshelter/wizards/core/Bundle");
    /* Logging object. */
    private Logger logger = Logger.getLogger(this.getClass().getName());
    
    /* Honor lists that support visual jList objects. */
    private List<Honor> received;
    private List<Honor> available;

    /**
     * Creates new form InvestitureDetailsVisualPanel
     */
    public InvestitureDetailsVisualPanel() {
        this(null, null, null, null, null);
    }

    /**
     * Creates new form InvestitureDetailsVisualPanel
     */
    public InvestitureDetailsVisualPanel(Pathfinder pathfinder, Lider lider,
            Club club, ProgressiveClass pClass, List<Honor> received) {
        this.received = ListSorter.sortHonorsByName(received);
        if(this.received == null) {
            this.received = new ArrayList<>();
        }
        initComponents();
        if(pathfinder != null) {
            PathfinderComboBox.setSelectedItem(pathfinder);
            PathfinderComboBox.setEnabled(false);
        }
        if(lider != null) {
            LiderComboBox.setSelectedItem(lider);
            LiderComboBox.setEnabled(false);
        }
        if(club != null) {
            ClubComboBox.setSelectedItem(club);
            ClubCheckBox.setSelected(true);
            ClubComboBox.setEnabled(true);
        }
        if(pClass != null) {
            ClassComboBox.setSelectedItem(pClass);
            ClassCheckBox.setSelected(true);
            ClassComboBox.setEnabled(true);
        }
        if(received != null && !received.isEmpty()) {
            HonorCheckBox.setSelected(true);
            AvailableList.setEnabled(true);
            ReceivedList.setEnabled(true);
        }
        actionPerformed(null);
    }

    @Override
    public String getName() {
        return WizardKeys.PROP_INVESTITURE_STEPS[1];
    }

    public List<Honor> getReceived() {
        return (HonorCheckBox.isSelected()? received : null);
    }

    public JComboBox getClassComboBox() {
        return ClassComboBox;
    }

    public JComboBox getClubComboBox() {
        return ClubComboBox;
    }

    public JComboBox getLiderComboBox() {
        return LiderComboBox;
    }

    public JComboBox getPathfinderComboBox() {
        return PathfinderComboBox;
    }
    
    public boolean hasCheckBoxSelected() {
        if(HonorCheckBox.isSelected() && received != null && !received.isEmpty()) {
            return true;
        } else {
            return (ClubCheckBox.isSelected() || ClassCheckBox.isSelected());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        PathfinderLabel = new javax.swing.JLabel();
        LiderLabel = new javax.swing.JLabel();
        PathfinderComboBox = new javax.swing.JComboBox();
        LiderComboBox = new javax.swing.JComboBox();
        ClubCheckBox = new javax.swing.JCheckBox();
        ClubSeparator = new javax.swing.JSeparator();
        ClubComboBox = new javax.swing.JComboBox();
        ClassCheckBox = new javax.swing.JCheckBox();
        ClassSeparator = new javax.swing.JSeparator();
        ClassComboBox = new javax.swing.JComboBox();
        HonorCheckBox = new javax.swing.JCheckBox();
        HonorSeparator = new javax.swing.JSeparator();
        AvailableScrollPane = new javax.swing.JScrollPane();
        AvailableList = new javax.swing.JList();
        IncludeButton = new javax.swing.JButton();
        WithdrawButton = new javax.swing.JButton();
        ReceivedScrollPane = new javax.swing.JScrollPane();
        ReceivedList = new javax.swing.JList();

        org.openide.awt.Mnemonics.setLocalizedText(PathfinderLabel, bundle.getString("InvestitureDetailsVisualPanel.PathfinderLabel.text"));

        org.openide.awt.Mnemonics.setLocalizedText(LiderLabel, bundle.getString("InvestitureDetailsVisualPanel.LiderLabel.text"));

        List<Pathfinder> pathfinders = loadPathfinderComboBoxData();
        PathfinderComboBox.setModel((pathfinders == null || pathfinders.isEmpty())? new DefaultComboBoxModel() : new DefaultComboBoxModel(pathfinders.toArray(new Pathfinder[pathfinders.size()])));
        PathfinderComboBox.setPreferredSize(new java.awt.Dimension(300, 20));
        PathfinderComboBox.setRenderer(new PathfinderRenderer());
        PathfinderComboBox.addActionListener(this);

        List<Lider> liders = loadLiderComboBoxData();
        LiderComboBox.setModel((liders == null || liders.isEmpty())? new DefaultComboBoxModel() : new DefaultComboBoxModel(liders.toArray(new Lider[liders.size()])));
        LiderComboBox.setPreferredSize(new java.awt.Dimension(300, 20));
        LiderComboBox.setRenderer(new LiderRenderer());
        LiderComboBox.addActionListener(this);

        org.openide.awt.Mnemonics.setLocalizedText(ClubCheckBox, bundle.getString("InvestitureDetailsVisualPanel.ClubCheckBox.text"));
        ClubCheckBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                ClubCheckBoxItemStateChanged(evt);
            }
        });

        List<Club> clubs = loadClubComboBoxData();
        ClubComboBox.setModel((clubs == null || clubs.isEmpty())? new DefaultComboBoxModel() : new DefaultComboBoxModel(clubs.toArray(new Club[clubs.size()])));
        ClubComboBox.setEnabled(false);
        ClubComboBox.setPreferredSize(new java.awt.Dimension(150, 20));
        ClubComboBox.setRenderer(new ClubRenderer());
        ClubComboBox.addActionListener(this);

        org.openide.awt.Mnemonics.setLocalizedText(ClassCheckBox, bundle.getString("InvestitureDetailsVisualPanel.ClassCheckBox.text"));
        ClassCheckBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                ClassCheckBoxItemStateChanged(evt);
            }
        });

        List<ProgressiveClass> classes = loadProgressiveClassComboBoxData();
        ClassComboBox.setModel((classes == null || classes.isEmpty())? new DefaultComboBoxModel() : new DefaultComboBoxModel(classes.toArray(new ProgressiveClass[classes.size()])));
        ClassComboBox.setEnabled(false);
        ClassComboBox.setPreferredSize(new java.awt.Dimension(150, 20));
        ClassComboBox.setRenderer(new ProgressiveClassRenderer());
        ClassComboBox.addActionListener(this);

        org.openide.awt.Mnemonics.setLocalizedText(HonorCheckBox, bundle.getString("InvestitureDetailsVisualPanel.HonorCheckBox.text"));
        HonorCheckBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                HonorCheckBoxItemStateChanged(evt);
            }
        });

        AvailableList.setModel(buildHonorListDataModel());
        AvailableList.setCellRenderer(new HonorRenderer());
        AvailableList.setEnabled(false);
        AvailableList.setMaximumSize(new java.awt.Dimension(200, 80));
        AvailableList.setMinimumSize(new java.awt.Dimension(200, 80));
        AvailableList.setPreferredSize(new java.awt.Dimension(200, 80));
        AvailableList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                AvailableListValueChanged(evt);
            }
        });
        AvailableScrollPane.setViewportView(AvailableList);

        org.openide.awt.Mnemonics.setLocalizedText(IncludeButton, bundle.getString("Buttons.Include"));
        IncludeButton.setEnabled(false);
        IncludeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                IncludeButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(WithdrawButton, bundle.getString("Buttons.Withdraw"));
        WithdrawButton.setEnabled(false);
        WithdrawButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                WithdrawButtonActionPerformed(evt);
            }
        });

        ReceivedList.setModel(buildReceivedHonorsListModel());
        ReceivedList.setCellRenderer(new HonorRenderer());
        ReceivedList.setEnabled(false);
        ReceivedList.setMaximumSize(new java.awt.Dimension(200, 80));
        ReceivedList.setMinimumSize(new java.awt.Dimension(200, 80));
        ReceivedList.setPreferredSize(new java.awt.Dimension(200, 80));
        ReceivedList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                ReceivedListValueChanged(evt);
            }
        });
        ReceivedScrollPane.setViewportView(ReceivedList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(ClubCheckBox)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ClubSeparator))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(ClassCheckBox)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ClassSeparator))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(LiderLabel)
                                            .addComponent(PathfinderLabel))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(PathfinderComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(LiderComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(21, 21, 21)
                                        .addComponent(ClubComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(21, 21, 21)
                                .addComponent(ClassComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(HonorCheckBox)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(HonorSeparator)))
                        .addGap(10, 10, 10))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(AvailableScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(IncludeButton)
                            .addComponent(WithdrawButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ReceivedScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(PathfinderLabel)
                            .addComponent(PathfinderComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(LiderLabel)
                            .addComponent(LiderComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(ClubCheckBox))
                    .addComponent(ClubSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ClubComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(ClassCheckBox)
                    .addComponent(ClassSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ClassComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(HonorCheckBox)
                    .addComponent(HonorSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(ReceivedScrollPane)
                            .addComponent(AvailableScrollPane))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGap(101, 101, 101)
                        .addComponent(IncludeButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(WithdrawButton)
                        .addContainerGap(119, Short.MAX_VALUE))))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void ClubCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_ClubCheckBoxItemStateChanged
        ClubComboBox.setEnabled(ClubCheckBox.isSelected());
        firePropertyChange(WizardKeys.PROP_ANY, 0, 1);
    }//GEN-LAST:event_ClubCheckBoxItemStateChanged

    private void ClassCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_ClassCheckBoxItemStateChanged
        ClassComboBox.setEnabled(ClassCheckBox.isSelected());
        firePropertyChange(WizardKeys.PROP_ANY, 0, 1);
    }//GEN-LAST:event_ClassCheckBoxItemStateChanged

    private void HonorCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_HonorCheckBoxItemStateChanged
        boolean isSelected = HonorCheckBox.isSelected();
        AvailableList.setEnabled(isSelected);
        ReceivedList.setEnabled(isSelected);
        if(!isSelected) {
            IncludeButton.setEnabled(false);
            WithdrawButton.setEnabled(false);
        }
        firePropertyChange(WizardKeys.PROP_ANY, 0, 1);
    }//GEN-LAST:event_HonorCheckBoxItemStateChanged

    private void IncludeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_IncludeButtonActionPerformed
        List<Honor> receiving = AvailableList.getSelectedValuesList();
        if(!receiving.isEmpty()) {
            received.addAll(receiving);
            received = ListSorter.sortHonorsByName(received);
            available.removeAll(receiving);
        }
        reloadLists();
        firePropertyChange(WizardKeys.PROP_ANY, 0, 1);
    }//GEN-LAST:event_IncludeButtonActionPerformed

    private void WithdrawButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_WithdrawButtonActionPerformed
        List<Honor> withdrawing = ReceivedList.getSelectedValuesList();
        if(!withdrawing.isEmpty()) {
            available.addAll(withdrawing);
            available = ListSorter.sortHonorsByName(available);
            received.removeAll(withdrawing);
        }
        reloadLists();
        firePropertyChange(WizardKeys.PROP_ANY, 0, 1);
    }//GEN-LAST:event_WithdrawButtonActionPerformed

    private void AvailableListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_AvailableListValueChanged
        if(AvailableList.isSelectionEmpty()) {
            IncludeButton.setEnabled(false);
        } else {
            ReceivedList.clearSelection();
            IncludeButton.setEnabled(true);
            WithdrawButton.setEnabled(false);
        }
    }//GEN-LAST:event_AvailableListValueChanged

    private void ReceivedListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_ReceivedListValueChanged
        if(ReceivedList.isSelectionEmpty()) {
            WithdrawButton.setEnabled(false);
        } else {
            AvailableList.clearSelection();
            WithdrawButton.setEnabled(true);
            IncludeButton.setEnabled(false);
        }
    }//GEN-LAST:event_ReceivedListValueChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList AvailableList;
    private javax.swing.JScrollPane AvailableScrollPane;
    private javax.swing.JCheckBox ClassCheckBox;
    private javax.swing.JComboBox ClassComboBox;
    private javax.swing.JSeparator ClassSeparator;
    private javax.swing.JCheckBox ClubCheckBox;
    private javax.swing.JComboBox ClubComboBox;
    private javax.swing.JSeparator ClubSeparator;
    private javax.swing.JCheckBox HonorCheckBox;
    private javax.swing.JSeparator HonorSeparator;
    private javax.swing.JButton IncludeButton;
    private javax.swing.JComboBox LiderComboBox;
    private javax.swing.JLabel LiderLabel;
    private javax.swing.JComboBox PathfinderComboBox;
    private javax.swing.JLabel PathfinderLabel;
    private javax.swing.JList ReceivedList;
    private javax.swing.JScrollPane ReceivedScrollPane;
    private javax.swing.JButton WithdrawButton;
    // End of variables declaration//GEN-END:variables

    @Override
    public void insertUpdate(DocumentEvent e) {
        changedUpdate(e);
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        changedUpdate(e);
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        firePropertyChange(WizardKeys.PROP_ANY, 0, 1);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        firePropertyChange(WizardKeys.PROP_ANY, 0, 1);
    }
    
    // <editor-fold defaultstate="collapsed" desc="Inner Classes">
    private class PathfinderRenderer extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if(value instanceof Pathfinder) {
                setText(((Pathfinder)value).getName());
            } else {
                setText("");
            }
            return this;
        }
    }
    
    private class LiderRenderer extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if(value instanceof Lider) {
                setText(((Lider)value).getName());
            } else {
                setText("");
            }
            return this;
        }
    }
    
    private class ClubRenderer extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if(value instanceof Club) {
                setText(((Club)value).getName());
            } else {
                setText("");
            }
            return this;
        }
    }
    
    private class ProgressiveClassRenderer extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if(value instanceof ProgressiveClass) {
                setText(((ProgressiveClass)value).getName());
            } else {
                setText("");
            }
            return this;
        }
    }
    
    private class HonorRenderer extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if(value instanceof Honor) {
                setText(((Honor)value).getName());
            } else {
                setText("");
            }
            return this;
        }
    }// </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="List Data Loading Code">
    private List loadPathfinderComboBoxData() {
        PathfinderController controller = Lookup.getDefault().lookup(PathfinderController.class);
        if(controller != null) {
            try{
                List list = controller.findActivePathfinderEntities();
                if(!list.isEmpty()) {
                    return list;
                }
            } catch(Exception ex) {
                logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
            }
        }
        return null;
    }
    
    private List loadLiderComboBoxData() {
        LiderController controller = Lookup.getDefault().lookup(LiderController.class);
        if(controller != null) {
            try{
                List list = controller.findActiveLiderEntities();
                if(!list.isEmpty()) {
                    return list;
                }
            } catch(Exception ex) {
                logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
            }
        }
        return null;
    }
    
    private List loadClubComboBoxData() {
        ClubController controller = Lookup.getDefault().lookup(ClubController.class);
        if(controller != null) {
            try{
                List list = controller.findClubEntities();
                if(!list.isEmpty()) {
                    return list;
                }
            } catch(Exception ex) {
                logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
            }
        }
        return null;
    }
    
    private List loadProgressiveClassComboBoxData() {
        ProgressiveClassController controller = Lookup.getDefault().lookup(ProgressiveClassController.class);
        if(controller != null) {
            try{
                List list = controller.findProgressiveClassEntities();
                if(!list.isEmpty()) {
                    return list;
                }
            } catch(Exception ex) {
                logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
            }
        }
        return null;
    }
    
    private DefaultListModel buildHonorListDataModel() {
        HonorController controller = Lookup.getDefault().lookup(HonorController.class);
        DefaultListModel model = new DefaultListModel();
        if(controller != null) {
            try{
                List<Honor> loaded = ListSorter.sortHonorsByName(controller.findHonorEntities());
                if(!loaded.isEmpty()) {
                    available = new ArrayList<>();
                    for(Honor honor : loaded) {
                        if(received == null || !received.contains(honor)) {
                            available.add(honor);
                            model.addElement(honor);
                        }
                    }
                }
            } catch(Exception ex) {
                logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
            }
        }
        return model;
    }
    
    private DefaultListModel buildReceivedHonorsListModel() {
        DefaultListModel model = new DefaultListModel();
        if(received != null) {
            for(Honor honor : received) {
                model.addElement(honor);
            }
        }
        return model;
    }
    
    private void reloadLists() {
        AvailableList.clearSelection();
        ReceivedList.clearSelection();
        WithdrawButton.setEnabled(false);
        IncludeButton.setEnabled(false);
        DefaultListModel honorModel = new DefaultListModel();
        for(Honor honor : available) {
            honorModel.addElement(honor);
        }
        AvailableList.setModel(honorModel);
        DefaultListModel receivedModel = new DefaultListModel();
        for(Honor honor : received) {
            receivedModel.addElement(honor);
        }
        ReceivedList.setModel(receivedModel);
    } // </editor-fold>
}
